package org.wu.framework.lazy.orm.database.jpa.repository.support.method;

import org.wu.framework.core.CamelAndUnderLineConverter;
import org.wu.framework.core.NormalUsedString;
import org.wu.framework.core.exception.RuntimeExceptionFactory;
import org.wu.framework.core.utils.ObjectUtils;
import org.wu.framework.lazy.orm.database.jpa.repository.enums.MethodName2SQLType;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class LazyUpdateMethodSupport extends AbstractLazyDynamicMethodSimpleQuerySupport{
    /**
     * 支持的方法转换类型
     *
     * @return MethodName2SQLType 集合
     */
    @Override
    protected List<MethodName2SQLType> supportMethodName2SQLTypeList() {
        return List.of(MethodName2SQLType.UPDATE,MethodName2SQLType.CHANGE);
    }

    /**
     * 方法名称转换成sql
     *
     * @param method 方法
     * @return sql
     */
    @Override
    public String methodName2SQL(Method method) {
        // updateNameAndAgeByIdAndIsDeleted
        String mainTableName = getMainTableName(method);
        String methodName = method.getName();

        // updateNameAndAgeByIdAndIsDeleted --> update_name_and_age_by_id_and_isDeleted
        String methodNameToLine = CamelAndUnderLineConverter.humpToLine2(methodName);

        MethodName2SQLType methodName2SQLType = getMethodName2SQLType(methodName);

        // update_name_and_age_by_id_and_isDeleted  name_and_age_by_id_and_isDeleted
        methodNameToLine = methodNameToLine.replaceAll(methodName2SQLType.getStartMethodNamePrefix(), NormalUsedString.EMPTY);
        // byString[0]=name_and_age  byString[1]=id_and_isDeleted
        String[] byString = methodNameToLine.split(methodKeyBy);


        // set
        String setColumnString = byString[0];
        if (setColumnString.isEmpty()) {
            RuntimeExceptionFactory.of("无法通过方法名:{%s}获取set属性", methodName);
        }


        String format = String.format("update %s ", mainTableName);

        // set 数据
        String setSql = Arrays
                .stream(setColumnString.split(methodKeyAnd))
                .filter(selectColumn -> !ObjectUtils.isEmpty(selectColumn))
                .map(setColumnName ->
                        NormalUsedString.SPACE + setColumnName +
                                NormalUsedString.SPACE + NormalUsedString.EQUALS +
                                NormalUsedString.SPACE + NormalUsedString.LEFT_BRACE + setColumnName + NormalUsedString.RIGHT_BRACE)
                .collect(Collectors.joining(NormalUsedString.COMMA));
        format += setSql;

        if (byString.length > 1) {


            format += NormalUsedString.SPACE + NormalUsedString.WHERE + NormalUsedString.SPACE;
            String condition = byString[1];
            // id_and_isDeleted  -->
            String[] columnList = condition.split(methodKeyAnd);

            String andList = Arrays.stream(columnList)
                    .filter(whereColumn -> !ObjectUtils.isEmpty(whereColumn))
                    .map(whereColumn ->
                            NormalUsedString.SPACE + whereColumn +
                                    NormalUsedString.SPACE + NormalUsedString.EQUALS +
                                    NormalUsedString.SPACE + NormalUsedString.LEFT_BRACE + whereColumn + NormalUsedString.RIGHT_BRACE
                    )
                    .collect(Collectors.joining(NormalUsedString.SPACE + NormalUsedString.AND + NormalUsedString.SPACE));
            return format + andList;
        } else {

            return format;
        }
    }
}
